package com.cloud.mall.sms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.input.SmsHomeNewProductInpDTO;
import com.cloud.mall.sms.input.SmsHomeRecommendProductInpDTO;
import com.cloud.mall.sms.mapper.SmsHomeNewProductMapper;
import com.cloud.mall.sms.mapper.SmsHomeRecommendProductMapper;
import com.cloud.mall.sms.model.SmsHomeNewProduct;
import com.cloud.mall.sms.model.SmsHomeNewProductExample;
import com.cloud.mall.sms.model.SmsHomeRecommendProduct;
import com.cloud.mall.sms.model.SmsHomeRecommendProductExample;
import com.cloud.mall.sms.out.SmsHomeNewProductOut;
import com.cloud.mall.sms.out.SmsHomeRecommendProductOut;
import com.cloud.mall.sms.service.SmsHomeNewProductService;
import com.cloud.mall.sms.service.SmsHomeRecommendProductService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SmsHomeRecommendProductServiceImpl implements SmsHomeRecommendProductService {
    @Autowired
    private SmsHomeRecommendProductMapper recommendProductMapper;

    @Transactional
    @Override
    public BaseResponse create(@RequestBody List<SmsHomeRecommendProductInpDTO> homeRecommendProductInpDTO) {
        List<SmsHomeRecommendProduct> homeRecommendProductList=MallBeanUtils.copyDoToDto(homeRecommendProductInpDTO, SmsHomeRecommendProduct.class);
        for (SmsHomeRecommendProduct recommendProduct : homeRecommendProductList) {
            recommendProduct.setRecommendStatus(1);
            recommendProduct.setSort(0);
            recommendProductMapper.insert(recommendProduct);
        }
        return BaseResponse.setResultSuccess(homeRecommendProductList.size());
    }

    @Override
    public BaseResponse updateRecommendStatus(List<Long> ids,Integer recommendStatus) {
        SmsHomeRecommendProductExample example = new SmsHomeRecommendProductExample();
        example.createCriteria().andIdIn(ids);
        SmsHomeRecommendProduct record = new SmsHomeRecommendProduct();
        record.setRecommendStatus(recommendStatus);
        int count=recommendProductMapper.updateByExampleSelective(record,example);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }


    @Transactional
    @Override
    public BaseResponse delete(@RequestBody List<Long> ids) {
        SmsHomeRecommendProductExample example = new SmsHomeRecommendProductExample();
        example.createCriteria().andIdIn(ids);
        return BaseResponse.setResultSuccess(recommendProductMapper.deleteByExample(example));
    }

    @Override
    public BaseResponse updateSort(Long id, Integer sort) {
        SmsHomeRecommendProduct recommendProduct = new SmsHomeRecommendProduct();
        recommendProduct.setId(id);
        recommendProduct.setSort(sort);
        int count= recommendProductMapper.updateByPrimaryKeySelective(recommendProduct);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }
    @Override
    public BaseResponse<CommonPage<SmsHomeRecommendProductOut>> list(String productName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        SmsHomeRecommendProductExample example = new SmsHomeRecommendProductExample();
        SmsHomeRecommendProductExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(productName)){
            criteria.andProductNameLike("%"+productName+"%");
        }
        if(recommendStatus!=null){
            criteria.andRecommendStatusEqualTo(recommendStatus);
        }
        example.setOrderByClause("sort desc");
        List<SmsHomeRecommendProduct> homeBrandList =recommendProductMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(homeBrandList, SmsHomeRecommendProductOut.class)));
    }


}
